package homework.newenergycharging.Work02.Reducer;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class Work02_Reducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
    private final DoubleWritable outValue = new DoubleWritable();

    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
        double sum = 0.0;

        // 遍历 values 并累加
        for (DoubleWritable value : values) {
            sum += value.get();
        }

        // 设置输出值
        outValue.set(sum);

        // 输出键值对
        context.write(key, outValue);
    }
}